home *** CD-ROM | disk | FTP | other *** search
- /*
- PatchTraps.c
-
- Patch various traps associated with events. The argument is a timeout, specifying how long to disable the trap.
- Supplying a value of zero re-enables the trap immediately. Each trap is disabled separately, but if timeout occurs
- all traps are re-enabled. The idea is that in normal use the timeout is a safety. Normally you'll disable the trap
- do something for a while, and then re-enable it. If disaster strikes, and your program is interrupted by an
- error before reenabling the traps then you might be stuck with a dead keyboard
- and mouse. Fortunately all you have to do is wait for the timeout and then everything will come back to life,
- sparing you the necessity of rebooting.
-
- My testing of these routines has been very limited. The Event-related patches seem to work ok,
- as does the GetMenuBar patch. The Munger trap patch crashes, but that's probably because of
- the printf statements.
-
- HISTORY:
- 4/7/97 dgp wrote it, based on AtExitToShell.c
- 4/8/97 dgp added time out
- */
- #ifndef _VIDEOTOOLBOX_
- #include "VideoToolbox.h"
- #endif
- #ifndef __TRAPS__
- #include <Traps.h>
- #endif
- #include <string.h>
- //<Events.h>
- //<Windows.h>
- //<OSUtils.h>
- pascal Boolean MyEventAvail(EventMask eventMask, EventRecord *theEvent);
- pascal Boolean MyGetNextEvent(EventMask eventMask, EventRecord *theEvent);
- pascal Boolean MyWaitNextEvent(EventMask eventMask, EventRecord *theEvent, UInt32 sleep, RgnHandle mouseRgn);
- pascal void MyBeginUpdate(WindowRef theWindow);
- pascal void MyEndUpdate(WindowRef theWindow);
- pascal void MyPrimeTime(QElemPtr tmTaskPtr,long count);
- pascal Handle MyGetMenuBar(void);
- pascal long MyMunger(Handle h,long offset,const void *ptr1, long len1,const void *ptr2,long len2);
- void PatchEventAvail(long ticks);
- void PatchGetNextEvent(long ticks);
- void PatchWaitNextEvent(long ticks);
- void PatchBeginUpdate(long ticks);
- void PatchEndUpdate(long ticks);
- void PatchPrimeTime(long ticks);
- void PatchGetMenuBar(long ticks);
- void PatchMunger(long ticks);
- static void RestoreEvents(void);
- static long timeout;
- #include <LowMem.h>
-
- void PatchEventAvail(long ticks)
- {
- static UniversalProcPtr oldTrapAddress=NULL,newUPP=NULL;
-
- if(ticks>0){
- timeout=ticks+LMGetTicks();
- if(oldTrapAddress==NULL){
- oldTrapAddress=(UniversalProcPtr)GetToolTrapAddress(_EventAvail);
- newUPP=NewRoutineDescriptor((ProcPtr)MyEventAvail,kPascalStackBased,GetCurrentISA());
- AtExitToShell(RestoreEvents);
- }
- SetToolTrapAddress((UniversalProcPtr)newUPP,_EventAvail);
- }else{
- if(oldTrapAddress!=NULL)
- SetToolTrapAddress((UniversalProcPtr)oldTrapAddress,_EventAvail);
- }
- }
-
- void PatchGetNextEvent(long ticks)
- {
- static UniversalProcPtr oldTrapAddress=NULL,newUPP=NULL;
-
- if(ticks>0){
- timeout=ticks+LMGetTicks();
- if(oldTrapAddress==NULL){
- oldTrapAddress=(UniversalProcPtr)GetToolTrapAddress(_GetNextEvent);
- newUPP=NewRoutineDescriptor((ProcPtr)MyGetNextEvent,kPascalStackBased,GetCurrentISA());
- AtExitToShell(RestoreEvents);
- }
- SetToolTrapAddress((UniversalProcPtr)newUPP,_GetNextEvent);
- }else{
- if(oldTrapAddress!=NULL)
- SetToolTrapAddress((UniversalProcPtr)oldTrapAddress,_GetNextEvent);
- }
- }
-
- void PatchWaitNextEvent(long ticks)
- {
- static UniversalProcPtr oldTrapAddress=NULL,newUPP=NULL;
-
- if(ticks>0){
- timeout=ticks+LMGetTicks();
- if(oldTrapAddress==NULL){
- oldTrapAddress=(UniversalProcPtr)GetToolTrapAddress(_WaitNextEvent);
- newUPP=NewRoutineDescriptor((ProcPtr)MyWaitNextEvent,kPascalStackBased,GetCurrentISA());
- AtExitToShell(RestoreEvents);
- }
- SetToolTrapAddress((UniversalProcPtr)newUPP,_WaitNextEvent);
- }else{
- if(oldTrapAddress!=NULL)
- SetToolTrapAddress((UniversalProcPtr)oldTrapAddress,_WaitNextEvent);
- }
- }
-
- void PatchBeginUpdate(long ticks)
- {
- static UniversalProcPtr oldTrapAddress=NULL,newUPP=NULL;
-
- if(ticks>0){
- timeout=ticks+LMGetTicks();
- if(oldTrapAddress==NULL){
- oldTrapAddress=(UniversalProcPtr)GetToolTrapAddress(_BeginUpDate);
- newUPP=NewRoutineDescriptor((ProcPtr)MyBeginUpdate,kPascalStackBased,GetCurrentISA());
- AtExitToShell(RestoreEvents);
- }
- SetToolTrapAddress((UniversalProcPtr)newUPP,_BeginUpDate);
- }else{
- if(oldTrapAddress!=NULL)
- SetToolTrapAddress((UniversalProcPtr)oldTrapAddress,_BeginUpDate);
- }
- }
-
- void PatchEndUpdate(long ticks)
- {
- static UniversalProcPtr oldTrapAddress=NULL,newUPP=NULL;
-
- if(ticks>0){
- timeout=ticks+LMGetTicks();
- if(oldTrapAddress==NULL){
- oldTrapAddress=(UniversalProcPtr)GetToolTrapAddress(_EndUpDate);
- newUPP=NewRoutineDescriptor((ProcPtr)MyEndUpdate,kPascalStackBased,GetCurrentISA());
- AtExitToShell(RestoreEvents);
- }
- SetToolTrapAddress((UniversalProcPtr)newUPP,_EndUpDate);
- }else{
- if(oldTrapAddress!=NULL)
- SetToolTrapAddress((UniversalProcPtr)oldTrapAddress,_EndUpDate);
- }
- }
-
- void PatchPrimeTime(long ticks)
- {
- static UniversalProcPtr oldTrapAddress=NULL,newUPP=NULL;
-
- if(ticks>0){
- timeout=ticks+LMGetTicks();
- if(oldTrapAddress==NULL){
- oldTrapAddress=(UniversalProcPtr)GetOSTrapAddress(_PrimeTime);
- newUPP=NewRoutineDescriptor((ProcPtr)MyPrimeTime,kPascalStackBased,GetCurrentISA());
- AtExitToShell(RestoreEvents);
- }
- SetOSTrapAddress((UniversalProcPtr)newUPP,_PrimeTime);
- }else{
- if(oldTrapAddress!=NULL)
- SetOSTrapAddress((UniversalProcPtr)oldTrapAddress,_PrimeTime);
- }
- }
-
- void PatchGetMenuBar(long ticks)
- {
- static UniversalProcPtr oldTrapAddress=NULL,newUPP=NULL;
-
- if(ticks>0){
- timeout=ticks+LMGetTicks();
- if(oldTrapAddress==NULL){
- oldTrapAddress=(UniversalProcPtr)GetToolTrapAddress(_GetMenuBar);
- newUPP=NewRoutineDescriptor((ProcPtr)MyGetMenuBar,kPascalStackBased,GetCurrentISA());
- AtExitToShell(RestoreEvents);
- }
- SetToolTrapAddress((UniversalProcPtr)newUPP,_GetMenuBar);
- }else{
- if(oldTrapAddress!=NULL)
- SetToolTrapAddress((UniversalProcPtr)oldTrapAddress,_GetMenuBar);
- }
- }
-
- static UniversalProcPtr oldMunger=NULL;
-
- void PatchMunger(long ticks)
- {
- static UniversalProcPtr oldTrapAddress=NULL,newUPP=NULL;
-
- if(ticks>0){
- timeout=ticks+LMGetTicks();
- if(oldTrapAddress==NULL){
- oldTrapAddress=(UniversalProcPtr)GetToolTrapAddress(_Munger);
- oldMunger=oldTrapAddress;
- newUPP=NewRoutineDescriptor((ProcPtr)MyMunger,kPascalStackBased,GetCurrentISA());
- AtExitToShell(RestoreEvents);
- }
- SetToolTrapAddress((UniversalProcPtr)newUPP,_Munger);
- }else{
- if(oldTrapAddress!=NULL)
- SetToolTrapAddress((UniversalProcPtr)oldTrapAddress,_Munger);
- }
- }
-
- static void RestoreEvents(void)
- {
- PatchEventAvail(0);
- PatchGetNextEvent(0);
- PatchWaitNextEvent(0);
- PatchBeginUpdate(0);
- PatchEndUpdate(0);
- PatchPrimeTime(0);
- PatchMunger(0);
- }
-
- pascal Boolean MyEventAvail(EventMask eventMask, EventRecord *theEvent)
- {
- eventMask;theEvent;
- //printf("MyEventAvail\n");
- if(LMGetTicks()>=timeout)RestoreEvents();
- return 0;
- }
-
- pascal Boolean MyGetNextEvent(EventMask eventMask, EventRecord *theEvent)
- {
- eventMask;theEvent;
- //printf("MyGetNextEvent\n");
- if(LMGetTicks()>=timeout)RestoreEvents();
- return 0;
- }
-
- pascal Boolean MyWaitNextEvent(EventMask eventMask, EventRecord *theEvent, UInt32 sleep, RgnHandle mouseRgn)
- {
- eventMask;theEvent;sleep;mouseRgn;
- //printf("MyWaitNextEvent\n");
- if(LMGetTicks()>=timeout)RestoreEvents();
- return 0;
- }
-
- pascal void MyBeginUpdate(WindowRef theWindow)
- {
- theWindow;
- //printf("MyBeginUpdate\n");
- if(LMGetTicks()>=timeout)RestoreEvents();
- }
-
- pascal void MyEndUpdate(WindowRef theWindow)
- {
- theWindow;
- //printf("MyEndUpdate\n");
- if(LMGetTicks()>=timeout)RestoreEvents();
- }
-
- pascal void MyPrimeTime(QElemPtr tmTaskPtr,long count)
- {
- tmTaskPtr;count;
- //printf("MyPrimeTime\n");
- if(LMGetTicks()>=timeout)RestoreEvents();
- }
-
- pascal Handle MyGetMenuBar(void)
- {
- //printf("MyGetMenuBar\n");
- if(LMGetTicks()>=timeout)RestoreEvents();
- return NULL;
- }
-
- pascal long MyMunger(Handle h,long offset,const void *ptr1, long len1,const void *ptr2,long len2)
- {
- char string[256];
- long size,result;
-
- PatchMunger(0); // printf calls Munger. So remove patch before using printf.
- printf("MyMunger\n");
- printf("h=0x%lx, (size h)=%ld, offset=%ld, ptr1=0x%lx, len1=%ld, ptr2=0x%lx, len2=%ld\n"
- ,h,GetHandleSize(h),offset,ptr1,len1,ptr2,len2);
- string[255]=0;
- size=GetHandleSize(h);
- if(size>sizeof(string)-1)size=sizeof(string)-1;
- strncpy(string,*h+offset,size);
- string[size]=0;
- printf("h=%s\n",string);
- strncpy(string,ptr1,len1);
- string[len1]=0;
- printf("ptr1=%s\n",string);
- strncpy(string,ptr2,len2);
- string[len2]=0;
- printf("ptr2=%s\n",string);
-
- result=Munger(h,offset,ptr1,len1,ptr2,len2);
-
- size=GetHandleSize(h);
- if(size>sizeof(string)-1)size=sizeof(string)-1;
- strncpy(string,*h+offset,size);
- string[size]=0;
- printf("h=%s\n",string);
- printf("result=%ld\n",result);
- PatchMunger(timeout-LMGetTicks());
- return result;
- }